package com.jy.adscm.wechat.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.service.ShCustomerInfoService;
import com.jy.adscm.wechat.service.WechatMaterialService;
import com.jy.adscm.wechat.service.WechatUserService;
import com.jy.adscm.wechat.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-09-29
 */
@RestController
@RequestMapping("/adscm/wechat/wechatMaterial")
public class WechatMaterialController {
    @Autowired
    WechatMaterialService wechatMaterialService;
    @Autowired
    private RedisOperator redis;
    @Autowired
    WechatUserService wechatUserService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody WechatMaterial wechatMaterial, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        WechatUser wechatUser = wechatUserService.selectByOpenId(openid);
        wechatMaterial.setCreator(wechatUser.getId().toString());
        wechatMaterial.setCreateTime(new Date());
        wechatMaterial.setIsOut(0);
        wechatMaterial.setIsSign(0);
        Boolean result=  wechatMaterialService.insert(wechatMaterial);
        return  new SimpleCoreResponse("200","新增成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody WechatMaterial wechatMaterial, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        Boolean result=  wechatMaterialService.updateWechatMaterial(wechatMaterial,openid);
        return  new SimpleCoreResponse("200","新增成功",result);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryWechatMaterial queryWechatMaterial, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        queryWechatMaterial.setOpenid(openid);
        Page<WechatMaterial> page=new Page<>(queryWechatMaterial.getCurrentPage(),queryWechatMaterial.getPageSize());
          page =  wechatMaterialService.selectWechatMaterial(page,queryWechatMaterial);
        return  new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectById/{id}")
    public SimpleCoreResponse selectById(@PathVariable Long id, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        WechatMaterial wechatMaterial =  wechatMaterialService.selectWechatMaterialById(id);
        return  new SimpleCoreResponse("200","查询成功",wechatMaterial);
    }


    @RequestMapping("selectInStock")
    public SimpleCoreResponse selectInStock(String  keywords,Integer pid, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(pid,null)){
            throw new AdscmSystemException("请传浆站ID");
        }
        List<WeChatResource>  resources =wechatMaterialService.selectInStock(keywords,openid,pid);
        return  new SimpleCoreResponse("200","查询成功",resources);
    }

}

